<?php

namespace App\Http\Controllers\Admin;

use DB;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class PlateController extends Controller
{

//    商品分区主页
    public function index()
    {
        $data = DB::table('plate')
            ->select('*', DB::raw("concat(path,',',id) as path_str"))
            ->orderBy('path_str')
            ->get();
        foreach ($data as $k => $v) {
            $num = substr_count($v->path, ',');
            $str = str_repeat('---------|', $num);
            $data[$k]->platename = $str . $v->platename;
        }
        return view('admin.plate.index', ['title' => '商品列表', 'data' => $data]);
    }
//      商品分类添加展示
    public function plateAdd()
    {
        $data = DB::table('plate')
            ->select('*')
            ->where('pid', '=', '0')
            ->orderBy('id')
            ->get();
        foreach ($data as $k => $v) {
            $num = substr_count($v->path, ',');
            $str = str_repeat('---------|', $num);
            $data[$k]->platename = $str . $v->platename;
        }
        return view('admin.plate.plateAdd', ['title' => '分类添加', 'data' => $data]);
    }
//    商品分类编辑操作
    public function plateDoAdd(Request $request)
    {
        $data = $request->except('_token');
        if ($data['pid'] == 0) {
            $data['path'] = 0;
        } else {
            $data['path'] = DB::table('plate')->where('id', $data['pid'])->first()->path . ',' . $data['pid'];
        }
        $old = DB::table('plate')->where('platename', $data['platename'])->first();
        if (!$old) {
            $res = DB::table('plate')->insert($data);
            if ($res) {
                return redirect('/admin/plate/index')->with(['info' => '添加成功']);
            } else {
                return back()->with(['info' => '添加失败']);
            }
        } else {
            return back()->with(['info' => '分区已存在']);
        }
    }

    public function plateEdit($id)
    {
        $data = DB::table('plate')->where('id', $id)->first();
        $alldata = DB::table('plate')->get();
        return view('/admin/plate/plateEdit', ['title'=>'分类编辑', 'data'=>$data, 'alldata'=>$alldata]);
    }

    public function plateDoEdit(Request $request)
    {
        $data = $request->except('_token');
        $res = DB::table('plate')->where('id', $data['id'])->update($data);
        if ($res) {
            return redirect('/admin/plate/index')->with(['info' => '修改成功']);
        } else {
            return back()->with(['info' => '修改失败']);
        }
    }

    public  function plateDoDelete($id)
    {
//        查找该商品分区下是否有别的分区
        $res1 = DB::table('plate')->where('pid', $id)->first();
//        查找该商品分区下是否有商品
        $res2 = DB::table('goods')->where('pid', $id)->first();
        if($res1) {
            return back()->with(['info'=>'禁止删除，该商品区有下属商品区']);
        }
        if($res2) {
            return back()->with(['info'=>'禁止删除，该商品区有售货中产品']);
        }
        $res3 = DB::table('plate')->where('id', $id)->delete();
        if ($res3) {
            return redirect('/admin/plate/index')->with(['info' => '删除成功','title'=>'商品列表']);
        } else {
            return back()->with(['info' => '删除失败','title'=>'商品列表']);
        }
    }

    public function goodsSearch(Request $request)
    {
        $data = DB::table('goods')
            ->where('goodsname','like','%'.$request->input('keywords', '').'%')
            ->paginate($request->input('num', 5));
        return view('admin.plate.searchGoods', ['title'=>'商品展示','data'=>$data,'request' => $request->all()]);

    }
}
